/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.rpc.service.impl.helper  
 */
package com.apache.rpc.service.impl.helper;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * description:  
 * @author Hou Dayu 创建时间：2014-11-3  
 * @Copyright(c)2014:
 */
public class SortList<E> {

	public void Sort(List<E> list, final String method, final String sort) {
		Collections.sort(list, new Comparator() {
			public int compare(Object a, Object b) {
				int ret = 0;
				try {
					Method m1 = ((E) a).getClass().getMethod(method, null);
					Method m2 = ((E) b).getClass().getMethod(method, null);
					if (sort != null && "desc".equals(sort)) {//倒序  
						ret = getSort(m2.invoke(((E) b), null).toString()).compareTo(
								getSort(m1.invoke(((E) a), null).toString()));
					} else {
						//正序  
						ret = getSort(m2.invoke(((E) a), null).toString()).compareTo(
								getSort(m1.invoke(((E) b), null).toString()));
					}
				} catch (NoSuchMethodException ne) {
					System.out.println(ne);
				} catch (IllegalAccessException ie) {
					System.out.println(ie);
				} catch (InvocationTargetException it) {
					System.out.println(it);
				}
				return ret;
			}
		});
	}

	private String getSort(String callType) {
		String result = "1";
		if ("socket".equals(callType)) {
			result = "2";
		} else if ("webservice".equals(callType)) {
			result = "3";
		} else if ("http".equals(callType)) {
			result = "5";
		} else if ("https".equals(callType)) {
			result = "4";
		}
		return result;
	}

}
